package com.gitee.ly.ymorm.transaction.jdbc;

import com.gitee.ly.ymorm.YmOrmConfig;
import com.gitee.ly.ymorm.log.YmOrmLog;
import com.gitee.ly.ymorm.log.YmOrmLogFactory;
import com.gitee.ly.ymorm.transaction.Transaction;
import com.gitee.ly.ymorm.transaction.TransactionFactory;
import com.gitee.ly.ymorm.transaction.TransactionIsolationLevel;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.UUID;

/**
 * YmOrmJdbcTransactionFactory
 *
 * @author ly
 */
public class JdbcTransactionFactory implements TransactionFactory {

    private YmOrmConfig ymOrmConfig;
    private YmOrmLog log = YmOrmLogFactory.getLog(this.getClass());

    public JdbcTransactionFactory(YmOrmConfig ymOrmConfig) {
        this.ymOrmConfig = ymOrmConfig;
    }

    @Override
    public Transaction newTransaction(Connection conn) {
        String uuid = UUID.randomUUID().toString();
        log.info("newTransaction[Transaction-uuid:" + uuid + "]");
        return new JdbcTransaction(uuid, ymOrmConfig, conn);
    }

    @Override
    public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
        String uuid = UUID.randomUUID().toString();
        log.info("newTransaction[Transaction-uuid:" + uuid + "]");
        return new JdbcTransaction(uuid, ymOrmConfig, dataSource, level, autoCommit);
    }
}
